//
//  PopulationSearchViewController.m
//  Utils
//
//  Created by enghou on 2018/4/24.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "PopulationSearchViewController.h"
#import "NetWorkerManager.h"
#import "QueryTypeSelectionController.h"
@interface PopulationSearchViewController ()

@end

@implementation PopulationSearchViewController

-(NSDictionary *)param{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if ([self.nameField.text length] > 0) {
        [param setObject:self.nameField.text forKey:@"searchVo.name"];
    }
    if ([self.idCardField.text length] > 0) {
        [param setObject:self.idCardField.text forKey:@"searchVo.idCardNo"];
    }
    if ([self.addressDetailField.text length] > 0) {
        [param setObject:self.addressDetailField.text forKey:@"searchVo.currentAddress"];
    }
    if (self.zhuxiaoModel) {
        //0未注销
        //1已注销
        [param setObject:self.zhuxiaoModel.ids forKey:@"searchVo.logOut"];
    }
    if (self.deathModel) {
        [param setObject:self.deathModel.ids forKey:@"searchVo.isDeath"];
    }
    if ([self.ruhuModel.ids length] > 0) {
        [param setObject:self.ruhuModel.ids forKey:@"searchVo.residentStatus"];
    }
    if ([self.sexModel.ids length] > 0) {
        [param setObject:self.sexModel.ids forKey:@"searchVo.gender"];
    }
    if ([self.minzuModel.ids length] > 0) {
        [param setObject:self.minzuModel.ids forKey:@"searchVo.nation"];
    }
    if ([self.marryModel.ids length] > 0) {
        [param setObject:self.marryModel.ids forKey:@"searchVo.maritalState"];
    }
    if ([self.politicalModel.ids length] > 0) {
        [param setObject:self.politicalModel.ids forKey:@"searchVo.politicalBackground"];
    }
    if ([self.eduModel.subTid length] > 0) {
        [param setObject:self.eduModel.subTid forKey:@"searchVo.schoolStr"];
    }
    if ([self.beginDateField.text length] > 0) {
        [param setObject:self.beginDateField.text forKey:@"searchVo.birthday"];
    }
    if ([self.endDateField.text length] > 0) {
        [param setObject:self.endDateField.text forKey:@"searchVo.endBirthday"];
    }
    return param;
}

-(void)query{
    if ([self.theDelete respondsToSelector:@selector(fetchModel:)]) {
        [self.theDelete fetchModel:[self param]];
        [self pop];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithTitle:@"查询" style:UIBarButtonItemStylePlain target:self action:@selector(query)];
    barButton.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = barButton;
    
    self.dateField = [[UITextField alloc]init];
    [self.view addSubview:self.dateField];
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 240)];
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.datePicker.backgroundColor = [UIColor whiteColor];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.minimumDate = [NSDate date];
    self.dateField.inputView = self.datePicker;
    self.dateField.inputAccessoryView = [self inputAccessView];
    
    self.endField = [[UITextField alloc]init];
    [self.view addSubview:self.endField];
    self.endField.inputView = self.datePicker;
    self.endField.inputAccessoryView = [self inputAccessView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)cancelAction{
    [self.view endEditing:YES];
}

-(void)sureAction{
    if ([self.dateField isFirstResponder]) {
        self.beginDateField.text = [[self.datePicker date]dateLineStrOnlyDay];
    }
    if ([self.endField isFirstResponder]) {
        self.endDateField.text = [[self.datePicker date]dateLineStrOnlyDay];
    }
    [self.view endEditing:YES];
}

-(void)chooseZhuXiao{
    if (!self.zhuxiaoModel) {
        self.zhuxiaoModel = [[PropertyDictItemModel alloc]init];
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *not = [UIAlertAction actionWithTitle:@"未注销" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.zhuxiaoModel.ids = @"0";
        self.zhuxiaoLabel.text = @"未注销";
    }];
    [alert addAction:not];
    UIAlertAction *yes = [UIAlertAction actionWithTitle:@"已注销" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.zhuxiaoModel.ids = @"1";
        self.zhuxiaoLabel.text = @"已注销";
    }];
    [alert addAction:yes];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)chooseDeath{
    if (!self.deathModel) {
        self.deathModel = [[PropertyDictItemModel alloc]init];
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *not = [UIAlertAction actionWithTitle:@"正常" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.deathModel.ids = @"0";
        self.deathLabel.text = @"正常";
    }];
    [alert addAction:not];
    UIAlertAction *yes = [UIAlertAction actionWithTitle:@"死亡" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.deathModel.ids = @"1";
        self.deathLabel.text = @"死亡";
    }];
    [alert addAction:yes];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)chooseRuHu{
    if (!self.states) {
        [self commonAlertWithContent:@"人户状态" choosenBlock:^(PropertyDictItemModel *model) {
            self.ruhuModel = model;
            self.ruhuLabel.text = self.ruhuModel.displayName;
        } getList:^(NSArray *array) {
            self.states = array;
        }];
    }else{
        [self alertWithArray:self.states choosenBock:^(PropertyDictItemModel *model) {
            self.ruhuModel = model;
            self.ruhuLabel.text = self.ruhuModel.displayName;
        }];
    }
}

-(void)chooseSex{
    if (self.sexArray) {
        [self alertWithArray:self.sexArray choosenBock:^(PropertyDictItemModel *model) {
            self.sexModel = model;
            self.sexLabel.text = self.sexModel.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"性别" choosenBlock:^(PropertyDictItemModel *model) {
            self.sexModel = model;
            self.sexLabel.text = self.sexModel.displayName;
        } getList:^(NSArray *array) {
            self.sexArray = array;
        }];
    }
}

-(void)chooseMinZu{
    if (self.minzuArray) {
        [self alertWithArray:self.minzuArray choosenBock:^(PropertyDictItemModel *model) {
            self.minzuModel = model;
            self.minzuLabel.text = self.minzuModel.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"民族" choosenBlock:^(PropertyDictItemModel *model) {
            self.minzuModel = model;
            self.minzuLabel.text = self.minzuModel.displayName;
        } getList:^(NSArray *array) {
            self.minzuArray = array;
        }];
    }
}

-(void)chooseMarry{
    if (self.marryArray) {
        [self alertWithArray:self.marryArray choosenBock:^(PropertyDictItemModel *model) {
            self.marryModel = model;
            self.marryLabel.text = model.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"婚姻状况" choosenBlock:^(PropertyDictItemModel *model) {
            self.marryModel = model;
            self.marryLabel.text = model.displayName;
        } getList:^(NSArray *array) {
            self.marryArray = array;
        }];
    }
}

-(void)choosePolitical{
    if (self.politicals) {
        [self alertWithArray:self.politicals choosenBock:^(PropertyDictItemModel *model) {
            self.politicalModel = model;
            self.politicalLabel.text = self.politicalModel.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"政治面貌" choosenBlock:^(PropertyDictItemModel *model) {
            self.politicalModel = model;
            self.politicalLabel.text = self.politicalModel.displayName;
        } getList:^(NSArray *array) {
            self.politicals = array;
        }];
    }
}

-(void)chooseEdu{
    QueryTypeSelectionController *query = (QueryTypeSelectionController *)[VCLoader vcWithName:@"QueryTypeSelectionController"];
    query.path = kEducationDegree;
    query.itemCls = @"DegreeListModel";
    query.theDelete = self;
    [self push:query];
}

-(void)fetchModel:(id)model{
    self.eduModel = model;
    self.eduLabel.text = self.eduModel.name;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        [self chooseZhuXiao];
    }else if (indexPath.row == 4){
        [self chooseDeath];
    }else if (indexPath.row == 5){
        [self chooseRuHu];
    }else if (indexPath.row == 6){
        [self chooseSex];
    }else if (indexPath.row == 7){
        [self chooseMinZu];
    }else if (indexPath.row == 8){
        [self chooseMarry];
    }else if (indexPath.row == 9){
        [self choosePolitical];
    }else if (indexPath.row == 10){
        [self chooseEdu];
    }
}


- (IBAction)showBeginDateAction:(id)sender {
    [self.dateField becomeFirstResponder];
}
- (IBAction)showEndDateAction:(id)sender {
    [self.endField becomeFirstResponder];
}

-(void)commonAlertWithContent:(NSString *)content choosenBlock:(void(^)(PropertyDictItemModel *))choosen getList:(void(^)(NSArray *))getList{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:content successBlock:^(BaseModel *model) {
        [self endLoading];
        if (model.success) {
            getList(model.list);
            [self alertWithArray:model.list choosenBock:choosen];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)alertWithArray:(NSArray <PropertyDictItemModel *>*)array choosenBock:(void(^)(PropertyDictItemModel *))choosen{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (PropertyDictItemModel *model in array) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:model.displayName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            choosen(model);
        }];
        [alert addAction:action];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}
@end
